/**
 *
 *  Util.Plot.TimeSeriesWindow, V0.7
 *
 *  Based on ptplot example. See below.
 *
 *  @author Michael Liljenstam
 *
 */


/* Top-level window containing a plotter.

 Copyright (c) 1998-1999 The Regents of the University of California.
 All rights reserved.
 Permission is hereby granted, without written agreement and without
 license or royalty fees, to use, copy, modify, and distribute this
 software and its documentation for any purpose, provided that the above
 copyright notice and the following two paragraphs appear in all copies
 of this software.

 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 ENHANCEMENTS, OR MODIFICATIONS.

                                        PT_COPYRIGHT_VERSION_2
                                        COPYRIGHTENDKEY
@ProposedRating Yellow (cxh@eecs.berkeley.edu)
@AcceptedRating Yellow (cxh@eecs.berkeley.edu)
*/

package SSF.Util.Plot;

//import ptolemy.plot.*;
//import ptolemy.plot.PlotPoint;
import ptolemy.gui.*;


import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import java.awt.event.*;
import java.io.*;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.Vector;

//////////////////////////////////////////////////////////////////////////
//// TimeSeriesWindow
/**

Based on PlotFrame in ptplot. (Name change from FlowPlotWindow.)<br><br>
Modifications made: <br>
<li> Removed the "Edit" menu. </li>
<li> Removed the "Open", "Save", and "Close" options under the "File" menu. </li>
<li> Removed the "Clear", and "SamplePlot" options under the "Special" menu. </li>
<li> Added functionality to handle multiple plots. </li>

@author mod's by Michael Liljenstam

@see ptolemy.plot.PlotFrame
@see ptolemy.plot.Plot
@see ptolemy.plot.PlotBox
@author Christopher Hylands and Edward A. Lee
@version $Id: TimeSeriesWindow.java,v 1.10 2003/02/13 19:34:38 mili Exp $
*/
public class TimeSeriesWindow extends Frame 
	implements TimeSeriesWindowCallback {

    ///////////////////////////////////////////////////////////////////
    ////                     constructors & methods                ////

    /** Construct a plot frame with a default title and by default contains
     *  an instance of Plot. After constructing this, it is necessary
     *  to call setVisible(true) to make the plot appear.
     */
    public TimeSeriesWindow() throws Exception {
        this("Ptolemy Plot Frame");
    }

    /** Construct a plot frame with the specified title and by default
     *  contains an instance of Plot. After constructing this, it is necessary
     *  to call setVisible(true) to make the plot appear.
     *  @param title The title to put on the window.
     */
    public TimeSeriesWindow(String title) throws Exception {
	//        this(title, null, 0);
	super("TimeSeries: " + title);
    }

    /** Construct a plot frame with the specified title and the specified
     *  instance of PlotBox.  After constructing this, it is necessary
     *  to call setVisible(true) to make the plot appear.
     *  @param title The title to put on the window.
     *  @param plotArg the plot object to put in the frame, or null to create
     *   an instance of Plot.
     */
    //    public TimeSeriesWindow(String title, MultiPlotBox[] plotArgs,
    //			    int plotHeights) 
    public TimeSeriesWindow(String title, TimeSeriesComponent plot)

	throws Exception {

	// invoke the base class constructor and pass in the argument a Plot
	// object. This makes sure that the plot field is an instance of
	// Plot class.
	super("TimeSeries: " + title);

        add("Center", plot); // add as a child object
	_plot = plot;

	installPlotMenu();

	setSize(_plot.getSize()); // why is this necessary?

	//	System.err.println("plot minsize" + _plot.getMinimumSize());
	//	System.err.println("plot size" + _plot.getSize());
	//	System.err.println("win minsize" + getMinimumSize());
	//	System.err.println("win size" + getSize());

	addClosingListenerAndDisplay();
    }

    ///////////////////////////////////////////////////////////////////
    ////                         public methods                    ////


    /**
     *  Do nothing. Overridden in subclass.
     */

    public void signalEndOfPlot()
    {
    }


    ///////////////////////////////////////////////////////////////////
    ////                         public variables                  ////


    ///////////////////////////////////////////////////////////////////
    ////                         constants                         ////

    // internal states
    protected final int   INIT = 0;               // inital state, before start
    protected final int   RUNNING = 1;            // mid-plot


    ///////////////////////////////////////////////////////////////////
    ////                         protected variables               ////


    /** @serial Menubar for this frame. */
    protected MenuBar _menubar = new MenuBar();

    /** @serial Plot menu for this frame. */
    protected Menu _plotMenu = new Menu("Plot");

    /** @serial directory that contains the input file. */
    protected String _directory = null;

    /** @serial internal state. */
    protected int _state = INIT;

    /** The internal container for the plot panels */
    protected TimeSeriesComponent _plot = null;


    ///////////////////////////////////////////////////////////////////
    ////                         protected methods                 ////

    /**
     *  Create a "light version" Plot menu.
     */

    protected void installPlotMenu() {
        // Plot menu
        MenuItem[] plotMenuItems = {
            // FIXME: These shortcuts are not right.
            new MenuItem("Fill", new MenuShortcut(KeyEvent.VK_F)),
            new MenuItem("Help", new MenuShortcut(KeyEvent.VK_H)),
            new MenuItem("Close", new MenuShortcut(KeyEvent.VK_W)),
        };
        PlotMenuListener sml = new PlotMenuListener();
        // Set the action command and listener for each menu item.
        for(int i = 0; i < plotMenuItems.length; i++) {
            plotMenuItems[i].setActionCommand(
                    plotMenuItems[i].getLabel());
            plotMenuItems[i].addActionListener(sml);
            _plotMenu.add(plotMenuItems[i]);
        }
        _menubar.add(_plotMenu);

        setMenuBar(_menubar);
    }


    /**
     *  Add closing window listener and display window.
     *  (finish setup.)
     */

    protected void addClosingListenerAndDisplay() {

	// Handle window closing by exiting the application.
	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    // Strangely, calling _close() here sends javac into
		    // an infinite loop (in jdk 1.1.4).
		    _close();
		    //		    System.exit(0);
		}
	    });
	setVisible(true);
    }


    /** Close the window.
     *  The window will signal the plotting component that it has been closed 
     *  (assuming that this means permanently).
     */
    protected void _close() {
	_plot.signalPlotWindowClosed();
        dispose();
    }

    /** Display more detailed information than given by _about().
     */
    protected void _help() {
        JOptionPane.showMessageDialog(this,
                "TimeSeriesWindow is a plot in a top-level window.\n" +
                "  File formats understood: Ptplot ASCII.\n" +
                "  Left mouse button: Zooming.",
                "About Ptolemy Plot", JOptionPane.INFORMATION_MESSAGE);
    }


    //
    // plot menu
    // 


    ///////////////////////////////////////////////////////////////////
    ////                         private methods                   ////



    ///////////////////////////////////////////////////////////////////
    ////                         inner classes                     ////


    class PlotMenuListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            MenuItem target = (MenuItem)e.getSource();
            String actionCommand = target.getActionCommand();
	    if (actionCommand.equals("Help")) {
                _help();
            } else if (actionCommand.equals("Fill")) {
		_plot.fillAll();
            } else if (actionCommand.equals("Close")) {
		_close();
	    }
        }
    }
}
